///@file CrossSection.cpp
///@brief Implementation of the class representing a cross section
///@author Arnaud Duval
///@version 0.0
///@date 2010/03/31

#include "../include/CrossSection.h"
#include "../include/Triangle.h"

///@brief Default constructor
CrossSection::CrossSection()
{
	A = 0.;
	Ixx = 0.;
	Iyy = 0.;
	Ixy = 0.;
	CGx = 0.;
	CGy = 0.;
	Iw = 0.;
	J = 0.;
	SHx = 0.;
	SHy = 0.;
}

///@brief Constructor form a closed polyline
///@param line Contour of the section. The points must be provided in trigonometric sense
///@warning Warping is not taken into account
CrossSection::CrossSection(ClosedPolyline line)
{
	Triangle *tri = new Triangle[line.Nbpts()];
	Point O(2);
	O.Coord(1) = 0.;
	O.Coord(2) = 0.;
	for(unsigned int i = 1 ; i <= line.Nbpts() ; i++)
	{
		if(i != line.Nbpts())
			tri[i-1] = Triangle(O, line[i], line[i+1]);
		else
			tri[i-1] = Triangle(O, line[i], line[1]);
	}
	Point* Gelem = new Point[line.Nbpts()];
	double *areas = new double[line.Nbpts()];
	Ixx = 0.;
	Iyy = 0.;
	Ixy = 0.;
	A = 0.;
	Iw = 0.;
	
	for(unsigned int i = 0 ; i < line.Nbpts() ; i++)
	{
		A += tri[i].Area();
		areas[i] = tri[i].Area();
		Gelem[i] = tri[i].Centroid();
		Ixx += tri[i].Ixx_O();
		Iyy += tri[i].Iyy_O();
		Ixy += tri[i].Ixy_O();
	}
	J = Ixx + Iyy;

	CGx = 0.;
	CGy = 0.;

	for(unsigned int i = 0 ; i < line.Nbpts() ; i++)
	{
		CGx += areas[i]*Gelem[i].Coord(1);
		CGy += areas[i]*Gelem[i].Coord(2);
	}

	CGx /= A;
	CGy /= A;
	SHx = CGx;
	SHy = CGy;


	delete[] tri;
	delete[] Gelem;
	delete[] areas;
}

///@brief Destructor
CrossSection::~CrossSection()
{

}

///@brief Return Area
double CrossSection::GetA()
{
	return A;
}

///@brief Return quadratic moment about x
double CrossSection::GetIxx()
{
	return Ixx;
}

///@brief Return quadratic moment about y
double CrossSection::GetIyy()
{
	return Iyy;
}

///@brief Return product of inertia
double CrossSection::GetIxy()
{
	return Ixy;
}

///@brief Return x coordinate of the centroid
double CrossSection::GetCGx()
{
	return CGx;
}

///@brief Return y coordinate of the centroid
double CrossSection::GetCGy()
{
	return CGy;
}

///@brief Return warping constant
double CrossSection::GetIw()
{
	return Iw;
}

///@brief Return torsional constant
double CrossSection::GetJ()
{
	return J;
}

///@brief Return x coordinate of shear center
double CrossSection::GetSHx()
{
	return SHx;
}

///@brief Return y coordinate of shear center
double CrossSection::GetSHy()
{
	return SHy;
}
	


